using System.Windows.Forms;
using System.Globalization;
using System.Threading;

namespace LogAnalyzer.Controls
{
    internal class LocalizedToolStripMenuItem : ToolStripMenuItem
    {

        private object[] _args = null;
        private string _resourceName = null;
        private string _text = null;

        public LocalizedToolStripMenuItem()
        {
            Globalization.ResourceManager.CultureChanged +=
                delegate(CultureInfo newCulture)
                {
                    SetBaseText();
                };
        }

        public object[] Arguments
        {
            get { return _args; }
            set
            {
                _args = value;
                SetBaseText();
            }
        }

        public string ResourceName
        {
            get { return _resourceName; }
            set
            {
                _resourceName = value;
                SetBaseText();
            }
        }

        public new string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                SetBaseText();
            }
        }

        private void SetBaseText()
        {
            if (string.IsNullOrEmpty(_text) == false)
            {
                base.Text = _text;
            }
            else if (string.IsNullOrEmpty(_resourceName) == false)
            {
                base.Text = GetLocalizedString();
            }
            else
            {
                base.Text = "[" + base.Name + "]";
            }
        }

        private string GetLocalizedString()
        {
            if (_resourceName == null)
            {
                return null;
            }
            else
            {
                try
                {
                    string res = Globalization.ResourceManager.Current.GetString(_resourceName, _args);
                    if (res == null)
                        res = "[" + _resourceName + "]";
                    return res;
                }
                catch
                {
                    return "[" + _resourceName + "]";
                }
            }
        }

    }
}
